package com.xjy.web.controller.system;

import com.xjy.common.annotation.Log;
import com.xjy.common.annotation.RepeatSubmit;
import com.xjy.common.core.controller.BaseController;
import com.xjy.common.core.domain.AjaxResult;
import com.xjy.common.core.domain.entity.SysUser;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.enums.BusinessType;
import com.xjy.common.exception.UtilException;
import com.xjy.common.utils.poi.ExcelUtil;
import com.xjy.system.domain.bo.SysImageMngAddBo;
import com.xjy.system.domain.bo.SysImageMngEditBo;
import com.xjy.system.domain.bo.SysImageMngQueryBo;
import com.xjy.system.domain.vo.SysImageMngVo;
import com.xjy.system.domain.vo.SysMaterialMngVo;
import com.xjy.system.service.ISysImageMngService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

import static com.xjy.common.utils.SecurityUtils.getLoginUser;

/**
 * 影像管理信息Controller
 *
 * @author zc
 * @date 2022-02-07
 */
@Api(value = "影像管理信息控制器", tags = {"影像管理信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/imagemng")
public class SysImageMngController extends BaseController {

	private final ISysImageMngService iSysImageMngService;

	/**
	 * 查询影像管理信息列表
	 */
	@ApiOperation("查询影像管理信息列表")
	@PreAuthorize("@ss.hasPermi('system:imagemng:list')")
	@GetMapping("/list")
	public TableDataInfo<SysImageMngVo> list(@Validated SysImageMngQueryBo bo) {
		return iSysImageMngService.queryPageList(bo);
	}


	/**
	 * 查询影像管理信息列表
	 */
	@ApiOperation("查询影像管理信息列表")
	@PreAuthorize("@ss.hasPermi('system:imagemng:list')")
	@GetMapping("/selectList")
	public TableDataInfo<SysImageMngVo> selectList(@Validated SysImageMngQueryBo bo) {
		if(null == bo){
			bo = new SysImageMngQueryBo();
		}
		bo.setAuditStatus(true);
		bo.setFileStatus(0L);
		return iSysImageMngService.queryPageList(bo);
	}
	/**
	 * 导出影像管理信息列表
	 */
	@ApiOperation("导出影像管理信息列表")
	@PreAuthorize("@ss.hasPermi('system:imagemng:export')")
	@Log(title = "影像管理信息", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<SysImageMngVo> export(@Validated SysImageMngQueryBo bo) {
		List<SysImageMngVo> list = iSysImageMngService.queryList(bo);
		ExcelUtil<SysImageMngVo> util = new ExcelUtil<SysImageMngVo>(SysImageMngVo.class);
		return util.exportExcel(list, "影像管理信息");
	}

	/**
	 * 获取影像管理信息详细信息
	 */
	@ApiOperation("获取影像管理信息详细信息")
	@PreAuthorize("@ss.hasPermi('system:imagemng:query')")
	@GetMapping("/{id}")
	public AjaxResult<SysImageMngVo> getInfo(@NotNull(message = "主键不能为空")
											 @PathVariable("id") Long id) {
		return AjaxResult.success(iSysImageMngService.queryById(id));
	}

	/**
	 * 新增影像管理信息
	 */
	@ApiOperation("新增影像管理信息")
	@PreAuthorize("@ss.hasPermi('system:imagemng:add')")
	@Log(title = "影像管理信息", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated @RequestBody SysImageMngAddBo bo) {
		SysUser user = getLoginUser().getUser();
		bo.setOperatorId(user.getUserId());
		bo.setOperator(user.getUserName());
		bo.setAuditStatus(false);
		return toAjax(iSysImageMngService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改影像管理信息
	 */
	@ApiOperation("修改影像管理信息")
	@PreAuthorize("@ss.hasPermi('system:imagemng:edit')")
	@Log(title = "影像管理信息", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping("/edit")
	public AjaxResult<Void> edit(@Validated @RequestBody SysImageMngEditBo bo) {

		return toAjax(iSysImageMngService.updateByEditBo(bo) ? 1 : 0);
	}


	/**
	 * 审核影像管理信息
	 */
	@ApiOperation("修改影像管理信息")
	@PreAuthorize("@ss.hasPermi('system:imagemng:edit')")
	@Log(title = "影像管理信息", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping("/audit")
	public AjaxResult<Void> audit(@Validated @RequestBody SysImageMngEditBo bo) {
		SysImageMngVo sysImageMngVo = iSysImageMngService.queryById(bo.getId());
		if (sysImageMngVo.getAuditStatus()) {
			throw new UtilException("该数据无需重新审批！");
		}
		bo.setAuditStatus(true);
		return toAjax(iSysImageMngService.updateByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 删除影像管理信息
	 */
	@ApiOperation("删除影像管理信息")
	@PreAuthorize("@ss.hasPermi('system:imagemng:remove')")
	@Log(title = "影像管理信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
								   @PathVariable Long[] ids) {
		return toAjax(iSysImageMngService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
	}
}
